package net.realsee.service.wcs.data.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.data.WcsSequence;
import net.realsee.domain.wcs.data.bo.WcsSequenceBo;
import net.realsee.domain.wcs.data.vo.WcsSequenceVo;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.data.WcsSequenceMapper;
import net.realsee.service.wcs.data.IWcsSequenceService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 序列(号)管理Service业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsSequenceServiceImpl extends ServiceImpl<WcsSequenceMapper, WcsSequence> implements IWcsSequenceService {

    private final WcsSequenceMapper baseMapper;

    /**
     * 查询序列(号)管理
     */
    @Override
    public WcsSequenceVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询序列(号)管理列表
     */
    @Override
    public TableDataInfo<WcsSequenceVo> selectVoPage(WcsSequenceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsSequence> lqw = buildVoQueryWrapper(bo);
        Page<WcsSequenceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询序列(号)管理列表
     */
    @Override
    public List<WcsSequenceVo> selectVoList(WcsSequenceBo bo) {
        LambdaQueryWrapper<WcsSequence> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsSequence> buildVoQueryWrapper(WcsSequenceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsSequence> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsSequence::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getFacility()), WcsSequence::getFacility, bo.getFacility());
        lqw.eq(StringUtils.isNotBlank(bo.getSequence()), WcsSequence::getSequence, bo.getSequence());
        lqw.like(StringUtils.isNotBlank(bo.getName()), WcsSequence::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), WcsSequence::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getSeparator()), WcsSequence::getSeparator, bo.getSeparator());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsSequence::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsSequence::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsSequence::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsSequence::getUnitId, bo.getUnitId());
        lqw.eq(bo.getDelFlag() != null, WcsSequence::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsSequence::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsSequence::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsSequence::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsSequence::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsSequence::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsSequence::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsSequence::getRowVersionStamp, bo.getRowVersionStamp());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), WcsSequence::getRemark, bo.getRemark());
        return lqw;
    }

    /**
     * 新增序列(号)管理
     */
    @Override
    public Boolean insertByBo(WcsSequenceBo bo) {
        WcsSequence add = BeanUtil.toBean(bo, WcsSequence.class);
        return this.save(add);
    }

    /**
     * 修改序列(号)管理
     */
    @Override
    public Boolean updateByBo(WcsSequenceBo bo) {
        WcsSequence update = BeanUtil.toBean(bo, WcsSequence.class);
        return this.updateById(update);
    }

    /**
     * 批量删除序列(号)管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
